package com.alibaba.middleware.hsf.hessian;

import com.alibaba.middleware.hsf.Data;

import com.taobao.hsf.com.caucho.hessian.io.AbstractHessianInput;
import com.taobao.hsf.com.caucho.hessian.io.AbstractHessianOutput;
import com.taobao.hsf.com.caucho.hessian.io.Hessian2Input;
import com.taobao.hsf.com.caucho.hessian.io.Hessian2Output;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * @author weipeng2k 2017年12月27日 上午11:06:08
 */
@State(Scope.Benchmark)
public class DeserialPerf {

    byte[] content;

    @Setup
    public void init() {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            AbstractHessianOutput out = new Hessian2Output(baos);
            out.writeObject(Data.gen());
            out.flush();
            content = baos.toByteArray();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    @Benchmark
    @BenchmarkMode(Mode.Throughput)
    public void deserial() throws Exception {
        ByteArrayInputStream bais = new ByteArrayInputStream(content);
        AbstractHessianInput in = new Hessian2Input(bais);
        in.readObject();
    }
}
